package com.springcloud.tools.conf;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/** redisson配置 */
@Configuration
public class RedissonManager {
  @Value("${spring.redis.cluster.nodes}")
  private String cluster;

  @Value("${spring.redis.password}")
  private String redisPd;

  @Value("${redis.auth}")
  private String auth;

  @Bean
  public RedissonClient getRedisson() {
    String[] nodes = cluster.split(",");
    // redisson版本是3.5，集群的ip前面要加上“redis://”，不然会报错，3.2版本可不加
    for (int i = 0; i < nodes.length; i++) {
      nodes[i] = "redis://" + nodes[i];
    }
    RedissonClient redisson = null;
    Config config = new Config();

    if ("1".equals(auth)) {
      config.useClusterServers().setScanInterval(2000).setPassword(redisPd).addNodeAddress(nodes);
    } else {
      config.useClusterServers().setScanInterval(2000).addNodeAddress(nodes);
    }
    redisson = Redisson.create(config);

    // 可通过打印redisson.getConfig().toJSON().toString()来检测是否配置成功
    return redisson;
  }
}
